import Base64 from "@/common/Base64Utils.js";
import config from "@/common/config.js";
import tools from "@/common/tools.js";
import common from "@/common/common.js";

const request = {};

request.upload = (filePath, params) => {
	uni.showLoading({
		mask: true,
		title: '努力上传中...'
	});
	
	params = tools.processParams(params);
	return uni.uploadFile({
		// url: config.uploadUrl,
		url: process.uniEnv.uploadUrl,
		filePath: filePath,
		name: 'files',
		formData: {
			'extra': params
		}
	}).then(res => {
		uni.hideLoading();
		let data = res.data;
		const dataType = Object.prototype.toString.call(data);
		if(dataType !== '[object Object]') {
			// params = JSON.stringify(params); //如果是对象则转换成字符串
			data = JSON.parse(data);
		}
		//console.log(data);
		if(data.errno!==0) {
			//console.log("----------------------")
			uni.showModal({
				title: '出错啦~',
				content: "上传失败了!!!",
				success() {
					//return {}
				}
			});
			//return false; //如果出现错误就返回空对象
		} else {
			//console.log("+++++++++++++++++++++++++++++")
			return data.data;
		}
	});
}

request.get = (path, params, options, unLoading) => {
	
	const curCustom = common.getCustomer()||{};
	/* const systemInfo = uni.getSystemInfoSync();
	console.log("----------", systemInfo) */
	const from = common.checkFromSource();
	// console.log("------from-----<<<", from)
	let headers = {
		'content-type':'application/x-www-form-urlencoded;charset=utf-8',
		'authtoken': config.AUTH_TOKEN, //权限token，很重要
		'openid': curCustom.openid||'', //用户openid
		'unionid': curCustom.unionid||'', //用户unionid
		'customid': curCustom.id||0, //用户id
		'nickname': tools.password(curCustom.nickname), //无法传中文,先转码
		'headimgurl': curCustom.headImgUrl||'', //用户头像
		'from': from, //来源
		'app-key': config.APP_KEY, //
		// 'apiCode': apiCode //请求接口,如:miniAuthService.getUserInfo
	}
	
	const shopData = common.getCurShop();
	if(shopData) {
		params.shopId = shopData.id;
	}
	params = tools.processParams(params);
	
	const isPost = (options && options.method=='post');
	//console.log(params+"====================="+isPost)
	
	if(!unLoading) {
		uni.showLoading({
			mask: true,
			title: '拼命加载中...'
		});	
	}
	
	return uni.request({
		url: process.uniEnv.reqUrl + path,
		header: headers,
		method: isPost?"POST":'GET',
		data: {params: params},
		// body: isPost?params:'',
	}).then(res => {
		uni.hideLoading();
		//uni.hideToast();
		// console.log("----<<",res);
		if(res.statusCode!=200) {
			/* uni.showToast({
				title: '出错：'+res[0].errMsg, icon:"none"
			}); */
			console.error(res[0].errMsg);
		} else {
			const data = res.data;
			if(data.code!=0) {
				console.error(data.msg);
				uni.showToast({
					title: data.msg, icon: "none"
				})
				//return false; //如果出现错误就返回空对象
			} else if(data.needLogin=="1") { //如果需要登陆
				uni.showToast({
					title: "请授权用户信息", icon: "none"
				})
				/* uni.navigateTo({
					url: "/pages/login/index"
				}) */
			} else {
				return data;
			}
		}
	}).catch((e) => {
		//console.error("请求出错", e);
		uni.showToast({
			title: '请求出错：'+e, icon:"none"
		});
		uni.hideLoading();
	});
}

export default request;